import { defineStore } from "pinia";
import { UserInfoVO } from "@/entiy/vo/UserInfoVO.ts";
import { VxeFormPropTypes } from "vxe-pc-ui";
import XEUtils from "xe-utils";
import RequestService from "@/service/RequestService.ts";
import { PageVO } from "@/entiy/vo/PageVO.ts";
import PromiseUtils from "@/utils/PromiseUtils.ts";
import { OrganizationInfoVO } from "@/entiy/vo/OrganizationInfoVO.ts";
import ArrayUtils from "@/utils/ArrayUtils.ts";

interface FormData {
    orgId: string,
    userId: string,
    userName: string,
    userAccount: string,
    userEmail: string,
}

interface EditFormData {
    orgId: string,
    orgName: string,
    userId: string,
    userName: string,
    account: string,
    userEmail: string,
}

export const useUserManagerStore = defineStore('useUserManagerStore', {
    state: () => {
        return {
            // 加载状态
            isPageLoading: false,
            isTableLoading: false,

            // 分页数据
            pageNum: 1,
            pageSize: 10,
            totalRow: 0,

            // 表格数据
            tableDataList: [] as Array<UserInfoVO>,

            // 表单数据
            formData: {} as FormData,

            // 表单校验规则
            formRules: {
                startDateTime: [
                    {required: true, content: '开始日期不能为空'},
                    {
                        validator({itemValue}) {
                            if (XEUtils.isNull(itemValue) || !XEUtils.isValidDate(new Date(itemValue))) {
                                return new Error('请输入一个合法的日期')
                            }
                        }
                    }
                ],

                endDateTime: [
                    {required: true, content: '结束日期不能为空'},
                    {
                        validator({itemValue}) {
                            if (XEUtils.isNull(itemValue) || !XEUtils.isValidDate(new Date(itemValue))) {
                                return new Error('请输入一个合法的日期')
                            }
                        }
                    }
                ]
            } as VxeFormPropTypes.Rules,

            // 机构信息列表
            orgInfoList: [] as Array<OrganizationInfoVO>,

            // 是否显示详情弹窗
            isShowDetailModel: false,

            // 是否显示编辑弹窗
            isShowEditModel: false,

            // 当前选择的用户信息
            selectedUserInfo: {} as UserInfoVO,

            // 编辑时的表单数据
            editFormData: {} as EditFormData,
        }
    },

    getters: {},

    actions: {
        // 查询用户列表
        async queryUserList(): Promise<void> {

            // 查询数据
            const data = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                orgId: this.formData.orgId,
                userId: this.formData.userId,
                userName: this.formData.userName,
                account: this.formData.userAccount,
                userEmail: this.formData.userEmail
            };

            const result = await RequestService.post<PageVO<UserInfoVO>>("/user-manage/get-user-info-list", data);
            if (result.isError) {
                return PromiseUtils.reject(result.errorMsg);
            }

            this.tableDataList = result.bizDta.recordList;
            this.totalRow = result.bizDta.totalRow;
            return PromiseUtils.resolve();
        },

        // 查询机构列表
        async queryOrgList(): Promise<void> {
            const result = await RequestService.post<Array<OrganizationInfoVO>>("/get-organization-info-list-by-name", null);
            if (result.isError) {
                return PromiseUtils.reject(result.errorMsg);
            }

            this.orgInfoList = result.bizDta;
            return PromiseUtils.resolve();
        },

        // 更新用户信息
        async updateUserInfo(): Promise<void> {
            this.selectedUserInfo.orgId = this.editFormData.orgId;
            this.selectedUserInfo.userName = this.editFormData.userName;
            this.selectedUserInfo.userEmail = this.editFormData.userEmail;

            const result = await RequestService.post<UserInfoVO>("/user-manage/update-user-info", this.selectedUserInfo);
            if (result.isError) {
                return PromiseUtils.reject(result.errorMsg);
            }

            // 更新表格数据
            const item = result.bizDta;
            ArrayUtils.update(this.tableDataList, "userId", item.userId, item);
            return PromiseUtils.resolve();
        },

        // 重置
        resetFormData() {
            // 重置表单
            this.formData = {} as FormData;
        },
    }
});
